<?php

namespace app\api\controller;


use app\admin\model\scenic\ScenicMediaCategory;
use app\api\model\scenic\Scenic as ScenicModel;
use app\api\model\scenic\ScenicCategory as ScenicCategoryModel;
use app\api\model\scenic\ScenicMedia;
use app\api\model\scenic\ScenicTickets;
use app\api\model\scenic\ScenicEvaluate as ScenicEvaluateModel;
use app\api\model\scenic\ScenicTicketsIcon;
use app\api\model\scenic\ScenicType;
use app\common\controller\Api;
use think\Db;

/**
 * 景区接口
 */
class Scenic extends Api
{
    protected $noNeedLogin = ['lists','category','recommend','mediacategory','ticketsicon'];
    protected $noNeedRight = ['*'];

    //增加抱团的转发次数
    public function addrepost()
    {
        $scenic_id = input('scenic_id',0);
        ScenicModel::where(['scenic_id'=>$scenic_id])->setInc('repost_num',1);
        $this->success('');
    }

    //推荐景区列表
    public function recommend()
    {
        //$lat = input('lat','');
        //$lng = input('lng','');
        $page_size = input('num',10);
        $city_id = input('city_id',0);

        $scenic_id = input('scenic_id','');
        $where = ['status'=>1,'is_recommend'=>1];
        if ($city_id){
            //$where['city_id'] = $city_id;
        }

        if ($scenic_id){
            $where['scenic_id'] = ['in',$scenic_id];
        }
        $list = ScenicModel::where($where)
            ->field('scenic_id,name,image,category_id,level,score,intro,lng,lat,city_id,comment_num')
            //->order('zan_num desc')
            ->order('is_top desc is_recommend desc')
            ->limit($page_size)
            ->select();

        foreach ($list as $k => $item) {

            /*if ($item[ 'lng' ] && $item[ 'lat' ]&&$lng&&$lat) {
                $distance = getDistance((float) $item[ 'lng' ], (float) $item[ 'lat' ], (float) $lng, (float) $lat);
                $list[ $k ][ 'distance' ] = round($distance / 1000,1);
            } else {
                $list[ $k ][ 'distance' ] = 0;
            }*/

            $tickets_list = ScenicTickets::where(['scenic_id'=>$item['scenic_id']])->column('price');

            $list[ $k ][ 'start_price' ] = count($tickets_list)>0?min($tickets_list):0;
        }

        $this->success('',$list);
    }

    //景区列表
    public function lists()
    {
        $lat = input('lat','');
        $lng = input('lng','');
        $page_size = input('page_size',10);
        $city_id = input('city_id',0);

        $keyword = input('keyword','');
        $category_id = input('category_id',0);
        $scenic_id = input('scenic_id','');
        $where = ['status'=>1];
        /*if ($city_id){
            $where['city_id'] = $city_id;
        }*/
        if ($category_id){
            $where[] = ['exp',Db::raw("FIND_IN_SET('$category_id',category_id)")];
        }

        if(!empty($keyword)){
            $where['name'] = ['like',"%$keyword%"];
        }
        if ($scenic_id){
            $where['scenic_id'] = ['in',$scenic_id];
        }
        $list = ScenicModel::where($where)
            ->field('scenic_id,name,image,category_id,level,score,intro,lng,lat,city_id,comment_num')
            ->order('zan_num desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        foreach ($list as $k => $item) {

            if ($item[ 'lng' ] && $item[ 'lat' ]&&$lng&&$lat) {
                $distance = getDistance((float) $item[ 'lng' ], (float) $item[ 'lat' ], (float) $lng, (float) $lat);
                $list[ $k ][ 'distance' ] = round($distance / 1000,1);
            } else {
                $list[ $k ][ 'distance' ] = 0;
            }

            $tickets_list = ScenicTickets::where(['scenic_id'=>$item['scenic_id']])->column('price');
            $list[ $k ][ 'start_price' ] = count($tickets_list)>0?min($tickets_list):0;
        }

        $this->success('',$list);

    }

    //景区详情
    public function info()
    {
        $lat = input('lat','');
        $lng = input('lng','');

        $scenic_id = input('scenic_id',0);

        $info = ScenicModel::where(['scenic_id'=>$scenic_id])->find();
        if ($info[ 'lng' ] && $info[ 'lat' ]&&$lng&&$lat) {
            $distance = getDistance((float) $info[ 'lng' ], (float) $info[ 'lat' ], (float) $lng, (float) $lat);
            $info[ 'distance' ] = round($distance / 1000,1);
        } else {
            $info[ 'distance' ] = 0;
        }
        $info[ 'comment_num' ] = ScenicEvaluateModel::where(['scenic_id'=>$scenic_id])->count();

        $info[ 'ticket_list' ] = ScenicTickets::where(['scenic_id'=>$scenic_id])->select();

        $info['media_img'] = ScenicMedia::where(['scenic_id'=>$scenic_id])
            ->column('media');
        /*$info['media_img'] = ScenicMedia::where(['scenic_id'=>$scenic_id])
            ->where('type','<>',5)
            ->column('media');

        $info['media_video'] = ScenicMedia::where(['scenic_id'=>$scenic_id])
            ->where('type',5)
            ->column('media');*/

        $this->success('',$info);
    }

    //景区媒体文件
    public function media()
    {
        $scenic_id = input('scenic_id',0);
        //$type_list = ScenicMedia::where(['scenic_id'=>$scenic_id])->group('type')->column('type');
        $category_list = ScenicMediaCategory::order('weigh desc')->field('id,name')->select();
        //$scenic_media_model = new ScenicMedia();
        //$type_text_list = $scenic_media_model->getTypelList();
        $list = [];
        foreach ($category_list as $k=>$v){
            $data = ScenicMedia::where(['scenic_id'=>$scenic_id,'category_id'=>$v['id']])
                ->field('id,scenic_id,type,title,describe,media')
                ->select();
            $list[] = [
                'type_text'=>$v['name'],
                'data'=>$data
            ];
        }
        $this->success('',$list);
    }

    //媒体文件详情
    public function mediainfo()
    {
        $id = input('id',0);
        $info = ScenicMedia::get($id);
        $this->success('',$info);
    }

    //景区类别
    public function category()
    {
        $type_list = ScenicType::order('weigh desc')->field('id,name')->select();
        foreach ($type_list as $k=>$v){
            $type_list[$k]['category'] = ScenicCategoryModel::where(['type_id'=>$v['id']])
                ->order('weigh desc')->field('id as category_id,name')->select();
        }
        $this->success('',$type_list);
    }

    //照片分类
    public function mediacategory()
    {
        $list = ScenicMediaCategory::order('weigh desc')->field('id,name')->select();
        $this->success('',$list);
    }

    //门票图标列表
    public function ticketsicon()
    {
        $list = ScenicTicketsIcon::order('weigh desc')->field('name,image')->select();
        $this->success('',$list);
    }

    //新增景区
    public function add()
    {
        try {
            $model = new ScenicModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->add($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }



    //我的景区列表
    public function mylists()
    {
        $where = ['status'=>1];

        $list = ScenicModel::where($where)
            ->field('scenic_id,name,image,category_id,level,score,intro,lng,lat,city_id')
            ->order('zan_num desc')
            ->select();

        $this->success('',$list);
    }


}